package edu.unipd.p2t.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.unipd.p2t.R;
import edu.unipd.p2t.connection.ConnectionManager;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {
    private final String TAG="IM";
    private CheckBox servizio;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //GESTIONE DEL CHECKBOX SERVIZIO
        servizio= (CheckBox) findViewById(R.id.checkservizio);
        servizio.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) {
                if (((CheckBox)v).isChecked())
                	startService( new Intent( MainActivity.this, ConnectionManager.class ) );
                else
                	stopService( new Intent( MainActivity.this, ConnectionManager.class ) );
            }
        });

        // define the list which holds the information of the list
        List<Map<String, Object>> resourceNames =
            new ArrayList<Map<String, Object>>();

        // define the map which will hold the information for each row
        Map<String, Object> data;

        // hard coded numbers retrieved from
        // http://code.google.com/android/reference/android/R.drawable.html
     
        data = new HashMap<String, Object>();
        data.put("text", "Pubblica Messaggio" );
        data.put("img", R.drawable.pubblica );
        resourceNames.add(data);
        data = new HashMap<String, Object>();
        data.put("text", "Leggi messaggi" );
        data.put("img", R.drawable.leggi );
        resourceNames.add(data);
        data = new HashMap<String, Object>();
        data.put("text", "Impostazioni" );
        data.put("img", R.drawable.imp );
        resourceNames.add(data);
        data = new HashMap<String, Object>();
        data.put("text", "Crediti" );
        data.put("img", R.drawable.crediti );
        resourceNames.add(data);
            
        SimpleAdapter notes = new SimpleAdapter(
            this,
            resourceNames,
            R.layout.mainrow,
            new String[] { "text", "img" },
            new int[] { R.id.text, R.id.img } );

        setListAdapter(notes);
	}
	

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
    	Log.i(TAG,"Hai cliccato su "+Integer.toString(position)+" ammetilo.");
    	switch(position)
    	{
    	case 0: startActivity(new Intent(this,MessageGui.class));
    		break;
    	case 1: startActivity(new Intent(this,MessagesList.class));
    		break;
    	case 2: startActivity(new Intent(this,Impostazioni.class));
    		break;
    	case 3: startActivity(new Intent(this,Crediti.class));
    		break;
    	}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	        Log.d(TAG, "back button pressed");
	        moveTaskToBack(true);
	        
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}

}
